import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;

public class UserPanel extends JPanel {
	public JButton scrollUp;
	public JButton scrollDown;
	public JPanel userList;
	public Output o;
	public ArrayList<UserBarPanel> userBarList;

	public UserPanel(Output arg) {
		o = arg;
		this.setBackground(new Color(73, 73, 73));
		this.setBorder(BorderFactory.createMatteBorder(0, 10, 14, 10,
				new Color(33, 33, 33)));
		this.setLayout(new BorderLayout());
		// c.fill = GridBagConstraints.VERTICAL;

		scrollUp = new JButton();
		scrollUp.setPreferredSize(new Dimension(196, 20));
		scrollUp.setIcon(new ImageIcon("uparrow.png"));
		scrollUp.addActionListener(new UpListener());
		this.add(scrollUp, BorderLayout.NORTH);

		scrollDown = new JButton();
		scrollDown.setPreferredSize(new Dimension(196, 20));
		scrollDown.setIcon(new ImageIcon("downarrow.png"));
		scrollDown.addActionListener(new DownListener());
		this.add(scrollDown, BorderLayout.SOUTH);

		userList = new JPanel();
		userList.setBackground(new Color(43, 43, 43));
		userList.setLayout(new GridLayout(15, 1, 0, 0));
		// userList.setPreferredSize(new Dimension(195, 400));
		this.add(userList, BorderLayout.CENTER);

		userBarList = new ArrayList<UserBarPanel>();
		for (int k = 0; k < 13; k++) {
			userBarList.add(new UserBarPanel());
			userList.add(userBarList.get(k));
		}

		userBarList.get(0).on("Apprentice", "Brocraft", 1500);
		userBarList.get(1).on("EmblemMarathon", "SueYang", 1366);
		userBarList.get(2).on("Captain_G2", "DirkFan", 1766);
		userBarList.get(3).on("Recruit", "McDonalds", 503);
		// userBarList.get(0).off();
		/*
		 * UserBar u0 = new UserBar(); u0.colorOn(); u0.setIcon("Apprentice");
		 * userList.add(u0);
		 * 
		 * UserBar u1 = new UserBar(); u1.colorOn();
		 * u1.setIcon("GunnerySergeant_G4"); userList.add(u1);
		 */
		/*
		 * UserBar u2 = new UserBar(); u2.setIcon("Lieutenant");
		 * userList.add(u2);
		 * 
		 * UserBar u3 = new UserBar(); u3.setIcon("Captain_G2");
		 * userList.add(u3);
		 * 
		 * UserBar u4 = new UserBar(); u4.setIcon("General_G4");
		 * userList.add(u4);
		 * 
		 * UserBar u5 = new UserBar(); u5.setIcon("EmblemMarathon");
		 * userList.add(u5);
		 * 
		 * UserBar u6 = new UserBar(); u6.setIcon("General_G3");
		 * userList.add(u6);
		 * 
		 * UserBar u7 = new UserBar(); u7.setIcon("Brigadier_G4");
		 * userList.add(u7);
		 */
	}

	public void populate() {
		System.out.println("populating");
		this.clear();
		this.set(0, o.username, Integer.parseInt(o.rating));

		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
		}

		Iterator<String> iter = o.muc.getOccupants();
		int i = 1;
		String s1 = "";
		String s2 = "000";
		String s3;
		while (iter.hasNext()) {
			s3 = iter.next();
			System.out.println(s3);
			StringTokenizer st = new StringTokenizer(StringUtils
					.parseResource(s3));
			s1 = st.nextToken();
			System.out.println(s1);
			if (!s1.equals(o.username)) {
				System.out.println("username: " + s1);
				if (st.hasMoreTokens())
					s2 = st.nextToken();
				o.userPanel.set(i, s1, Integer.parseInt(s2));
				i += 1;
			}
		}
		System.out.println("populated");
	}

	public void test() {
		userBarList.get(0).on("Apprentice", "Brocraft", 1500);
		userBarList.get(1).on("EmblemMarathon", "SueYang", 1366);
		userBarList.get(2).on("Captain_G2", "DirkFan", 1766);
		userBarList.get(3).on("Recruit", "McDonalds", 503);
	}

	public void clear() {
		for (int k = 0; k < 13; k++) {
			userBarList.get(k).off();
		}
	}

	public void set(int k, String arg2, int arg3) {
		if (arg3 < 800)
			userBarList.get(k).on("Recruit", arg2, arg3);
		else if (arg3 < 1000)
			userBarList.get(k).on("Apprentice", arg2, arg3);
		else if (arg3 < 1500)
			userBarList.get(k).on("Captain_G2", arg2, arg3);
		else if (arg3 < 3000)
			userBarList.get(k).on("EmblemMarathon", arg2, arg3);
	}

	private class UpListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.out.println("ScrollUp");
		}
	}

	private class DownListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.out.println("ScrollDown");
		}
	}
}
